﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Bll;
using DBA.Web.UI;

namespace DBA.Web.BookManage
{
    public partial class TeacherBookList : AdminPage 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            Bind(1, null);
        }



        protected void linkBtnSearch_Click(object sender, EventArgs e)
        {
            IDictionary<String, String> dict = new Dictionary<String, String>();
            dict.Add("ps_PaperName", txtps_Name.Text.Trim());
            dict.Add("ps_Class", txtps_Class.Text.Trim());
            dict.Add("ps_PublicationName", txtps_PublicationName.Value.Trim());
            dict.Add("ps_BeginTime", txtps_BeginTime.Value.Trim());
            dict.Add("ps_EndTime", txtps_EndTime.Value.Trim());       
            ViewState["conditions"] = dict;
            Bind(1, dict);
        }

        protected void ddlBind()
        {
            //BaseCodeDDL.DdlInit(ddlXi, "Xi");//绑定系
            //BaseCodeDDL.DdlInit(ddlSuo, "Suo");
            //BaseCodeDDL.DdlInit(ddlDept, "sys_department");
            //BaseCodeDDL.DdlInit(ddlJszwzg, "JSZWZG");

        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            IDictionary<String, String> dict = null;
            if (ViewState["conditions"] != null)
            {
                dict = (IDictionary<String, String>)ViewState["conditions"];
            }
            Bind(e.NewPageIndex, dict);
        }

        private void Bind(int pageIdx, IDictionary<String, String> dict)
        {

            string sql = "select a.*,  ";
            sql += "attCount=(select count(1) from PorjectAttachment b where a.ps_ID=b.Pap_ps_ID) from PaperSum3 a  ";

            if (Request.Params["type"] != null)
            {
                if (Request.Params["type"] == "1")
                {
                    sql += " left join EmployeeInfo b on a.ps_Person1=b.Name and Person1Is=1 ";
                    sql += " left join EmployeeInfo c on a.ps_Person2=c.Name and Person2Is=1 ";
                    sql += " left join EmployeeInfo d on a.ps_Person3=d.Name and Person3Is=1 ";
                    sql += " left join EmployeeInfo e on a.ps_Person4=e.Name and Person4Is=1 ";
                    sql += " left join EmployeeInfo f on a.ps_Person5=f.Name and Person5Is=1 ";
                }
            }
            sql += " where 1=1  ";
            //sql += " and (a.ps_Person1 = @ps_Person or a.ps_Person2 = @ps_Person or a.ps_Person3 = @ps_Person or a.ps_Person4 = @ps_Person or a.ps_Person5 = @ps_Person) ";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
           

            if (Request.Params["type"] != null)
            {
                if (Request.Params["type"] == "1")
                {
                    sql += " and (b.xi_id=@xi_id or  c.xi_id=@xi_id or  d.xi_id=@xi_id or  e.xi_id=@xi_id or f.xi_id=@xi_id)";
                    dal.ManagerParameter.AddNewInParameter("xi_id", ServerContext.AdminCurrentUser.Xi_ID);
                }
            }
            else
            {
                sql += " and ((a.ps_Person1 = @ps_Person  and Person1Is=1) or (a.ps_Person2 = @ps_Person and Person2Is=1) or (a.ps_Person3 = @ps_Person and Person3Is=1) or (a.ps_Person4 = @ps_Person and Person4Is=1) or (a.ps_Person5 = @ps_Person and Person5Is=1)) ";
                dal.ManagerParameter.AddNewInParameter("ps_Person", ServerContext.AdminCurrentUser.UserName);
            }
            if (dict != null)
            {
                if (!dict["ps_PaperName"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_PaperName like '%'+@ps_PaperName+'%'";
                    dal.ManagerParameter.AddNewInParameter("ps_PaperName", dict["ps_PaperName"].ToString());
                }

                if (!dict["ps_Class"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_Class = @ps_Class";
                    dal.ManagerParameter.AddNewInParameter("ps_Class", dict["ps_Class"].ToString());
                }

                if (!dict["ps_PublicationName"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_PublicationName like '%'+@ps_PublicationName+'%'";
                    dal.ManagerParameter.AddNewInParameter("ps_PublicationName", dict["ps_PublicationName"].ToString());
                }

                if (!dict["ps_BeginTime"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_date >= @ps_BeginTime";
                    dal.ManagerParameter.AddNewInParameter("ps_BeginTime", dict["ps_BeginTime"].ToString());
                }

                if (!dict["ps_EndTime"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_date <= @sp_EndTime";
                    dal.ManagerParameter.AddNewInParameter("sp_EndTime", dict["ps_EndTime"].ToString());
                }             
            }


            int iCount = 0;

            replData.DataSource = dal.ModifyData.GetPageDataTable("(" + sql + ")", "*", "", "ps_ID DESC", pageIdx, 10, ManagerSQL.PageViewTableType.MixtureTable, ref iCount);
            replData.DataBind();
            AspNetPager1.RecordCount = iCount;

        }

        protected void linkBtnMaskSearch_Click(object sender, EventArgs e)
        {
            //IDictionary<String, String> dict = new Dictionary<String, String>();
            //dict.Add("UserName", txtMaskMemberId.Text.Trim());
            //ViewState["conditions"] = dict;
            //Bind(1, dict);
        }

        protected void linkBtnDeleSelect_Click(object sender, EventArgs e)
        {
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            if (!String.IsNullOrEmpty(hidDeleString.Value))
            {
                string value = hidDeleString.Value;
                string sql = "delete from [PaperSum3] where ps_ID in (" + value + ");";
                dal.ExecuteNonQuery(sql);
                if (String.IsNullOrEmpty(dal.ErrorMessage))
                {
                    Bind(1, null);
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.correct('数据删除成功');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.error('数据删除失败');", true);
                }
            }
        }
    }
}